package cn.duli.xianer.controller;

import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.WantGoodsPicture;
import cn.duli.xianer.service.WantGoodsPictureService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wantGoodsPicture")
public class WantGoodsPictureController {

    @Autowired
    private WantGoodsPictureService wantGoodsPictureService;


    /**
     * 新增求购商品图片
     * @param
     * @return
     */
    @PostMapping(value = "/save")
    @RequiresRoles("business")
    public MyResult save(@RequestBody List<WantGoodsPicture> wantGoodsPictureList) {
        for (int i=0;i<wantGoodsPictureList.size();i++) {
            wantGoodsPictureService.save(wantGoodsPictureList.get(i));
        }
        return new MyResult(200, "新增求购商品图片集成功",null,null,null,null);
    }

    /**
     * 根据图片id删除求购图片
     * @param wgpId
     */
    @RequestMapping("/deleteById/{wgpId}")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "business"})
    public MyResult deleteById(@PathVariable("wgpId") Integer wgpId) {
        wantGoodsPictureService.deleteById(wgpId);
        return new MyResult(200, "根据图片id删除求购图片",null,null,null,null);
    }


    /**
     * 根据求购图片id修改该图片信息
     * @param wantGoodsPictureList
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/updateById")
    @RequiresRoles("business")
    public MyResult updateById(@RequestBody List<WantGoodsPicture> wantGoodsPictureList) throws IOException {
        for (int i=0;i<wantGoodsPictureList.size();i++) {
            // 已经新增的数据
            if (wantGoodsPictureList.get(i).getWgpId() != 0) {
                // 根据图片id修改图片
                wantGoodsPictureService.updateById(wantGoodsPictureList.get(i));
            }
        }
        return new MyResult(200, "根据图片id修改该图片信息",null,null,null,null);
    }

    /**
     * 修改主图
     * @param wantGoodsPicture
     */
    @RequestMapping("/updateMainPic")
    @RequiresRoles("business")
    public MyResult updateMainPic(WantGoodsPicture wantGoodsPicture) {
        wantGoodsPictureService.updateMainPic(wantGoodsPicture);
        return new MyResult(200, "修改主图",null,null,null,null);
    }


    /**
     *根据求购商品id查询此求购商品下的所有图片
     * @param wgpWgid
     * @return
     */
    @GetMapping("/findByGoodsId/{wgpWgid}")
    public MyResult findByGoodsId(@PathVariable("wgpWgid") Integer wgpWgid) {
        List<WantGoodsPicture> list = wantGoodsPictureService.findByGoodsId(wgpWgid);
        return new MyResult(200, "根据求购商品id查询此求购商品下的所有图片",null,list,null,null);
    }

    /**
     * 根据求购商品id查询此求购商品的主图
     * @param wgpWgid
     * @return
     */
    @GetMapping("/findMainPicByGoodsId/{wgpWgid}")
    public MyResult findMainPicByGoodsId(@PathVariable("wgpWgid") Integer wgpWgid) {
        WantGoodsPicture wantGoodsPicture = wantGoodsPictureService.findMainPicByGoodsId(wgpWgid);
        return new MyResult(200, "根据求购商品id查询此求购商品的主图",null,null,wantGoodsPicture,null);
    }

    /**
     * 分页查询所有求购图片
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    @RequiresRoles("admin")
    public MyResult findAll(@RequestParam(defaultValue = "1") int pageNo,
                                          @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoodsPicture> pageInfo = new PageInfo<>(wantGoodsPictureService.findAll());
        return new MyResult(200, "分页查询所有求购图片",null,null,null,pageInfo);
    }

    /**
     * 根据求购图片id批量删除求购图片
     * @param wgpId
     */
    @RequestMapping("/batchDelete")
    @RequiresRoles("admin")
    public MyResult batchDelete(@RequestParam(value = "wgpId") String wgpId) {
        String[] gpIds = wgpId.split(",");
        List<Integer> list = new ArrayList<>();
        for(String str:gpIds){
            list.add(Integer.parseInt(str));
        }
        wantGoodsPictureService.batchDelete(list);
        return new MyResult(200, "根据求购图片id批量删除求购图片",null,null,null,null);
    }


    /**
     * 根据求购图片id查询该求购图片详情
     * @param wgpId
     * @return
     */
    @GetMapping("/findById/{wgpId}")
    public MyResult findById(@PathVariable("wgpId") Integer wgpId) {
        WantGoodsPicture wantGoodsPicture = wantGoodsPictureService.findById(wgpId);
        return new MyResult(200, "根据求购图片id查询该求购图片详情",null,null,wantGoodsPicture,null);
    }
}
